x = int(input()) 
if x > 999 and x < 10000:

    n = 1
    m = 10000
    maximum = 0

    for i in range (0, 4):
        r = x % m
        for j in range (i, 4):
            chislo = r // n
            prostoe = True
            for z in range (2, chislo // 2):
                if chislo % z == 0:
                  prostoe = False
                  break
            if prostoe == True and chislo > maximum:
                maximum = chislo
            n = n * 10
        m = m // 10
        n = 1

    print(maximum)

else :
    print (0)
